package com.leo.utils

import android.app.Activity
import android.content.Context
import android.graphics.drawable.Drawable
import android.support.annotation.LayoutRes
import android.support.v4.content.ContextCompat
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast

fun ViewGroup.inflate(@LayoutRes layoutRes: Int, attachToRoot: Boolean = false): View {
    return LayoutInflater.from(context).inflate(layoutRes, this, attachToRoot)
}

fun Activity.toast(str: String) {
    toast(this, str)
}

fun android.support.v4.app.Fragment.toast(str: String) {
    activity?.let {
        toast(it, str)
    }
}

fun String.opt(): String {
    return if (this.isNullOrEmpty()) {
        ""
    } else {
        this
    }
}

fun toast(ctx: Context, str: String) {
    Toast.makeText(ctx, str, Toast.LENGTH_SHORT).show()
}

fun EditText.string(): String {
    return this.text?.toString() ?: ""
}

fun TextView.string(): String {
    return this.text?.toString() ?: ""
}

fun TextView.isEmpty(): Boolean {
    return this.string().isEmpty()
}

fun EditText.isEmpty(): Boolean {
    return this.string().isEmpty()
}

fun Context.color(color: Int): Int {
    return ContextCompat.getColor(this, color)
}

fun Activity.color(color: Int): Int {
    return ContextCompat.getColor(this, color)
}

fun Context.drawable(drawable: Int): Drawable? {
    return ContextCompat.getDrawable(this, drawable)
}

fun android.support.v4.app.Fragment.color(color: Int): Int? {
    return activity?.let { ContextCompat.getColor(it, color) }
}

fun <T : Collection<Any>> T?.check(func: (T) -> Unit, funNull: () -> Unit) {
    if (this != null && this.isNotEmpty()) {
        func(this)
    } else {
        funNull()
    }
}

fun <T : Collection<Any>> T?.check(func: (T) -> Unit) {
    check(func) {}
}

inline fun <reified T> T.debug(log: Any) {
    Log.d(T::class.simpleName, log.toString())
}

fun View.setColor(color: Int) {
    setBackgroundColor(context.color(color))
}


fun Double.nail(nail: Int = 2, prefix: String = "", suffix: String = "") = "$prefix${String.format("%.${nail}f", this)}$suffix"


fun currentTime(): Long {
    return System.currentTimeMillis() / 1000
}

fun <T1 : Any, T2 : Any, R : Any> safeLet(p1: T1?, p2: T2?, block: (T1, T2) -> R?): R? {
    return if (p1 != null && p2 != null) block(p1, p2) else null
}

fun <T1 : Any, T2 : Any, T3 : Any, R : Any> safeLet(p1: T1?, p2: T2?, p3: T3?, block: (T1, T2, T3) -> R?): R? {
    return if (p1 != null && p2 != null && p3 != null) block(p1, p2, p3) else null
}

fun <T1 : Any, T2 : Any, T3 : Any, T4 : Any, R : Any> safeLet(p1: T1?, p2: T2?, p3: T3?, p4: T4?, block: (T1, T2, T3, T4) -> R?): R? {
    return if (p1 != null && p2 != null && p3 != null && p4 != null) block(p1, p2, p3, p4) else null
}

fun <T1 : Any, T2 : Any, T3 : Any, T4 : Any, T5 : Any, R : Any> safeLet(p1: T1?, p2: T2?, p3: T3?, p4: T4?, p5: T5?, block: (T1, T2, T3, T4, T5) -> R?): R? {
    return if (p1 != null && p2 != null && p3 != null && p4 != null && p5 != null) block(p1, p2, p3, p4, p5) else null
}
